package com.wujunshen.algorithm.leetcode.basic;

import java.util.HashMap;
import java.util.Map;

/**
 * @author frank woo(吴峻申) <br>
 * email:<a href="mailto:frank_wjs@hotmail.com">frank_wjs@hotmail.com</a> <br>
 * @date 2022/7/28 20:06<br>
 */
public class 稀疏矩阵 {
   private Map<Integer, Map<Integer, Integer>> map = new HashMap<>();
    public 稀疏矩阵(int[][] mat1){
        //Map<行标, Map<列标，值>>
        for(int i = 0; i < mat1.length; i++){
            map.put(i, new HashMap<>());
            for(int j = 0; j < mat1[i].length; j++){
                if(mat1[i][j] != 0){
                    map.get(i).put(j, mat1[i][j]);
                }
            }
        }
    }
    public int[][] multiply(int[][] mat1, int[][] mat2) {
        Map<Integer, Map<Integer, Integer>> map1 = new HashMap<>();
        Map<Integer, Map<Integer, Integer>> map2 = new HashMap<>();
        //构造矩阵1的哈希表
        //矩阵1的哈希表格式Map<行标, Map<列标，值>>
        for(int i = 0; i < mat1.length; i++){
            map1.put(i, new HashMap<>());
            for(int j = 0; j < mat1[i].length; j++){
                if(mat1[i][j] != 0){
                    map1.get(i).put(j, mat1[i][j]);
                }
            }
        }
        //构造矩阵2的哈希表
        //矩阵2的哈希表格式Map<列标, Map<行标，值>>
        for(int i = 0; i < mat2.length; i++){
            for(int j = 0; j < mat2[i].length; j++){
                if(mat2[i][j] != 0){
                    if(!map2.containsKey(j)) {
                        map2.put(j, new HashMap<>());
                    }
                    map2.get(j).put(i, mat2[i][j]);
                }
            }
        }

        int[][] res = new int[mat1.length][mat2[0].length];
        for(int i = 0; i < mat1.length; i++){
            for(int j = 0; j < mat2[0].length; j++){
                Map<Integer, Integer> row = map1.get(i);
                Map<Integer, Integer> col = map2.get(j);
                for(int rowIndex : row.keySet()){
                    if(col != null && col.containsKey(rowIndex)) {
                        res[i][j] += row.get(rowIndex) * col.get(rowIndex);
                    }
                }
            }
        }
        return res;
    }

    public int[][] add(int[][] mat1, int[][] mat2) {
        if (mat1.length!=mat2.length||mat1[0].length!=mat2[0].length){
            return new int[][]{};
        }
        稀疏矩阵 a=new 稀疏矩阵(mat1);
        稀疏矩阵 b=new 稀疏矩阵(mat2);
        //构造矩阵1的哈希表
        int[][] res = new int[mat1.length][mat1[0].length];
        for(int i = 0; i < mat1.length; i++){
            for(int j = 0; j < mat1[0].length; j++){
                //Map<行标, Map<列标，值>>
                Map<Integer, Integer> row =a.map.get(i);
                Map<Integer, Integer> col = b.map.get(i);

                if(row.containsKey(j)^col.containsKey(j)){
                    if(row.containsKey(j)){
                        res[i][j] = row.get(j) ;
                    }else{
                        res[i][j] = col.get(j) ;
                    }
                }else{
                    if(row.containsKey(j)){
                        res[i][j] = row.get(j) + col.get(j);
                    }else{
                        res[i][j] =0;
                    }

                }
            }
        }
        return res;
    }
}
